package com.hotel_management_backstage.lin.service;

import com.hotel_management_backstage.lin.dao.SysRoomMapper;
import com.hotel_management_backstage.lin.entity.SysRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SysRoomService {
    @Autowired
    private SysRoomMapper sysRoomMapper;

    public SysRoom selectById(int id){
        return sysRoomMapper.selectById(id);
    }

    public List<SysRoom> selectAll(){
        return sysRoomMapper.selectAll();
    }

    public void insertRoom(SysRoom sysRoom){
        sysRoom.setRoomReserved("否");
        sysRoomMapper.insertRoom(sysRoom);
    }

    public void deleteRoomById(int id){
        sysRoomMapper.deleteRoomById(id);
    }

    public SysRoom selectRoomByRoomNumber(int roomNumber){
        return sysRoomMapper.selectRoomByRoomNumber(roomNumber);
    }

    @Transactional
    public void updateRoom(SysRoom sysRoom){
        SysRoom sysRoom1 = sysRoomMapper.selectRoomByRoomNumber(sysRoom.getRoomNumber());
        sysRoom1.setRoomInformation(sysRoom.getRoomInformation());
        sysRoom1.setRoomIntroduction(sysRoom.getRoomIntroduction());
        sysRoom1.setRoomReserved(sysRoom.getRoomReserved());
        sysRoomMapper.updateRoom(sysRoom1);
    }

    public List<SysRoom> selectAllFreeRoom(){
        List<SysRoom> sysRooms = sysRoomMapper.selectAll();
        List<SysRoom> allFreeRoom = new ArrayList<>();
        for(SysRoom s : sysRooms){
            if(Objects.equals(s.getRoomReserved(), "否")){
                allFreeRoom.add(s);
            }
        }
        return allFreeRoom;
    }

    public int deleteRoomByRoomNumber(int roomNumber){
        SysRoom sysRoom = sysRoomMapper.selectRoomByRoomNumber(roomNumber);
        if(Objects.equals(sysRoom.getRoomReserved(), "是")){
            return 0; //0代表无法删除，房间正在使用中
        }else {
            sysRoomMapper.deleteRoomByRoomNumber(roomNumber);
            return 1; //1代表删除成功！
        }
    }

}
